#include "dbclient.h"



void DBClient::ReciveData ( i32 bytes )
{
	ENTER_CS ( connection->p_mx_brecv );
	u32 size = connection->p_buff_recv.size();
	signed char *adr = &connection->p_buff_recv[0];

	if ( size < SIZ_HEADER )
	{
		LEAVE_CS ( connection->p_mx_brecv );
		return;         // data to short
	}

	dbi_header h;
	memcpy ( &h, adr, SIZ_HEADER );

	if ( h.len > connection->p_buff_recv.size() )
	{
		LEAVE_CS ( connection->p_mx_brecv );
		return;         // data to short
	}

	switch ( h.type )
	{
		case DBI_WELCOME:
		{
			hndlr_welcome ( h );
			break;
		}

		case DBI_MDATA:             // client writes measurment data
		{
			hndlr_mdata ( h );
			break;
		}
	}

	LEAVE_CS ( connection->p_mx_brecv );
}

i32 DBClient::hndlr_welcome ( dbi_header h )
{
	dbi_welcome w;
	signed char *adr = &connection->p_buff_recv[0];

	if ( h.len < SIZ_HEADER + SIZ_WELCOME )
	{
		return 0;
	}

	dbi_header h2;
	popBuffer ( &h2, SIZ_HEADER );
	popBuffer ( &w, SIZ_WELCOME );
	DBTime cli_time;
	cli_time.FromSystemTime ( w.my_time.time );
	LOGP3 ( "@@NET:WELCOME[%s][%s]", w.client_version_txt, cli_time.TimeDateToString().c_str() );
	/// response to 'welcome'
	int login = CONFIG->CheckDataBaseUser ( w.hash_user_name, w.hash_user_pass );   /// TODO : Implement this function
	dbi_w_response wr;
	int res = 0;

	if ( login == 0 )
	{
		LOGP1 ( "## User/Passowrd invalid." );
		wr.response = DBI_RESPONSE_LOGIN_FAILED;
		wr.server_time.time = time ( 0 );
		wr.server_time.milisecond = 0;
		wr.server_ver = SRVR_VER;
		strcpy ( wr.server_version_txt, SRVR_VERTXT );
		h2.len = SIZ_RESPONSE + SIZ_HEADER;
		h2.type = DBI_RESPONSE;
		AddBuffer ( &h2, SIZ_HEADER );
		AddBuffer ( &wr, SIZ_RESPONSE );
		res = FlushBuffer();
		permision = PERM_NO;
		/// TODO : erase client
	}

	if ( login == 1 )
	{
		LOGP1 ( "## User/Passowrd OK." );
		wr.response = DBI_RESPONSE_OK;
		wr.server_time.time = time ( 0 );
		wr.server_time.milisecond = 0;
		wr.server_ver = SRVR_VER;
		strcpy ( wr.server_version_txt, SRVR_VERTXT );
		h2.len = SIZ_RESPONSE + SIZ_HEADER;
		h2.type = DBI_RESPONSE;
		AddBuffer ( &h2, SIZ_HEADER );
		AddBuffer ( &wr, SIZ_RESPONSE );
		res = FlushBuffer();
		permision = PERM_RW;
	}

	if ( login == 2 )
	{
		LOGP1 ( "## User/Passowrd OK [READONLY]." );
		wr.response = DBI_RESPONSE_USER_ONLY_READ;
		wr.server_time.time = time ( 0 );
		wr.server_time.milisecond = 0;
		wr.server_ver = SRVR_VER;
		strcpy ( wr.server_version_txt, SRVR_VERTXT );
		h2.len = SIZ_RESPONSE + SIZ_HEADER;
		h2.type = DBI_RESPONSE;
		AddBuffer ( &h2, SIZ_HEADER );
		AddBuffer ( &wr, SIZ_RESPONSE );
		res = FlushBuffer();
		permision = PERM_RO;
	}

	if ( !res )
	{
		// TODO erase client here
		connection->Close ( 0 );        // if connection is closed do not produce i
		active = 0;
		EXCEX ( 0xAFAF, "Error sending response" );
	}
}

i32 DBClient::hndlr_mdata ( dbi_header h )
{
	dbi_mdata md;
	popBuffer ( &h, SIZ_HEADER );
	popBuffer ( &md, SIZ_MDATA );
	u32 ch_id, dev_id;
	numChannel ( md.channel, dev_id, ch_id );
        dbi_header h2;

	if ( permision != PERM_RW )
	{
	    LOGP1("SERVER: Client tried to write data, without RW permisions. (Non-fatal)");

        h2.type = DBI_ERR;
        h2.len = SIZ_ERR + SIZ_HEADER;
        dbi_error err;
        err.err_code = 0x0001;
        err.module = 0x0001;
        strncpy(err.txt,"SERVER: No write permision",MAX_ERRSTR);

        AddBuffer(&h2,SIZ_HEADER);
        AddBuffer(&err,SIZ_ERR);
        if (!FlushBuffer())
        {
            LOGP1("Error while sending error to client:P");
            active = 0;
            return 0;
        }

	}

	LOGP1("WRITE HERE !");




}

